var oWCTeams = {};
var oWCTeamsSorted = {};

var oWCMatches = {};

var bIsFirstTime = false;

function init(){
    bIsFirstTime = isFirstTime();
    translateUI();
    $("#cmb_my_team").change(onMyTeamChanged);
    $("#chk_follow_my_team").change(onChkFollowMyTeamChanged);
    $("#chk_follow_my_group_teams").change(onChkFollowMyGroupTeamsChanged);
    $("#chk_follow_all_teams").change(onChkFollowAllTeamsChanged);    
}


function translateUI(){
    var follow_my_team = localStorage['follow_my_team'];
    if (!follow_my_team) {
        localStorage.setObject('follow_my_team', 1);
        follow_my_team = 1;
    }
    
    var follow_my_group_teams = localStorage['follow_my_group_teams'];
    if (!follow_my_group_teams) {
        localStorage.setObject('follow_my_group_teams', 1);
        follow_my_group_teams = 1;
    }
    
    
    var follow_all_teams = localStorage['follow_all_teams'];
    if (!follow_all_teams) {
        localStorage.setObject('follow_all_teams', 0);
        follow_all_teams = 0;
    }
    
    
    if (follow_my_team == 1) {
        $("#chk_follow_my_team").attr("checked", true);
    }
    
    if (follow_my_group_teams == 1) {
        $("#chk_follow_my_group_teams").attr("checked", true);
    }
    
    if (follow_all_teams == 1) 
        $("#chk_follow_all_teams").attr("checked", true);
    
    
    getTeamConfig();
    
    $("span").each(function(index, item){
        setTextUI(item.id);
    })
    
    $("#config_txt_close").attr("value", getUI("config_txt_close"));
    $("#config_txt_close").click(onCloseClick);
}

function onCloseClick(){
    window.close();
}

// Get all Team Names from Locale Config
function getTeamConfig(){
    var strTeamUrl = "";
    
    strTeamUrl = getUI('main_url_feed_2') + feed_teams;
    
    var xhr = new XMLHttpRequest();
    xhr.open("GET", strTeamUrl + "?t=" + getCacheNumber(), false);
    
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4) {
            var xmlString = "";
            var json = "";
            try {
                xmlString = xhr.responseXML.xml ? xhr.responseXML.xml : (new XMLSerializer()).serializeToString(xhr.responseXML);
            } 
            catch (e) {
                xmlString = xhr.responseText;
            }
            
            json = $.xml2json(xmlString);
            
            
            if (json != "") {
                oWCTeams = new Array(json.Data.Team.length);
                oWCTeamsSorted = new Array();
                for (var i = 0; i < json.Data.Team.length; i++) {
                    oWCTeams[json.Data.Team[i].ShortName] = json.Data.Team[i];
                    oWCTeamsSorted.push(json.Data.Team[i].ShortName);
                }
                localStorage.setObject('wcTeams', oWCTeams);
                localStorage.setObject('wcTeamsSorted', oWCTeamsSorted);
                loadMyTeamCombo();
            }
        }
    }
    xhr.send();
}




function loadMyTeamCombo(){
    var options = '';
    options += '<option value="---|---|---">' + getUI("config_txt_all_teams") + '</option>';
    for (var i = 0; i < oWCTeamsSorted.length; i++) {
        options += '<option value="' + oWCTeams[oWCTeamsSorted[i]].code + '|' + oWCTeams[oWCTeamsSorted[i]].ShortName + '|' + oWCTeams[oWCTeamsSorted[i]].FullName + '">' + oWCTeams[oWCTeamsSorted[i]].FullName + '</option>';
    }
    $("#cmb_my_team").html(options);
    var my_team = localStorage['my_team'];
    if (!my_team || my_team == '---|---|---') {
        localStorage.setItem('my_team', $("#cmb_my_team").val());
        
        localStorage.setObject('follow_my_team', 0);
        localStorage.setObject('follow_my_group_teams', 0);
        localStorage.setObject('chk_follow_all_teams', 0);
        
        $("#div_follow_my_team :input").attr("disabled", true);
        $("#div_follow_my_team :input").removeAttr("checked");
        $("#div_follow_my_team :span").attr("disabled", true);
        $("#div_follow_my_team :span").attr("class", "disabled");
        
        
        $("#div_follow_my_group_teams :input").attr("disabled", true);
        $("#div_follow_my_group_teams :input").removeAttr("checked");
        $("#div_follow_my_group_teams :span").attr("disabled", true);
        $("#div_follow_my_group_teams :span").attr("class", "disabled");
        
        $("#team_flag").hide();
    }
    else {
        $("#cmb_my_team").val(my_team);
        $("#div_follow_my_team :span").removeAttr("disabled");
        $("#div_follow_my_team :span").removeAttr("class");
        $("#div_follow_my_team :input").removeAttr("disabled");
        
        $("#div_follow_my_group_teams :span").removeAttr("disabled");
        $("#div_follow_my_group_teams :span").removeAttr("class");
        $("#div_follow_my_group_teams :input").removeAttr("disabled");
        
        $("#team_flag").attr("src", String.format(flag_pattern_s, $("#cmb_my_team").val().split("|")[1]));
        $("#team_flag").show();
        
    }
}

function onMyTeamChanged(){
	trackEvent("MyTeam",$("#cmb_my_team").val().split("|")[1]);
	
    if ($("#cmb_my_team").val() == '---|---|---') {
        $("#div_follow_my_team :input").removeAttr("checked");
        $("#div_follow_my_team :span").attr("disabled", true);
        $("#div_follow_my_team :span").attr("class", "disabled");
        
        $("#div_follow_my_group_teams :input").removeAttr("checked");
        $("#div_follow_my_group_teams :span").attr("disabled", true);
        $("#div_follow_my_group_teams :span").attr("class", "disabled");
        
        $("#team_flag").hide();
    }
    else {
        if (localStorage['my_team'] == '---|---|---') {
            localStorage.setObject('follow_my_team', 1);
            localStorage.setObject('follow_my_group_teams', 1);
            $("#chk_follow_my_team").attr("checked", true);
            $("#chk_follow_my_group_teams").attr("checked", true);
        }
        $("#div_follow_my_team :span").removeAttr("disabled");
        $("#div_follow_my_team :span").removeAttr("class");
        $("#div_follow_my_team :input").removeAttr("disabled");
        
        $("#div_follow_my_group_teams :span").removeAttr("disabled");
        $("#div_follow_my_group_teams :span").removeAttr("class");
        $("#div_follow_group_teams :input").removeAttr("disabled");
        
        
        $("#team_flag").attr("src", String.format(flag_pattern_s, $("#cmb_my_team").val().split("|")[1]));
        $("#team_flag").show();
    }
    localStorage.setItem('my_team', $("#cmb_my_team").val());
    
    refreshExtensionIcon();
    
	chrome.extension.sendRequest({
        getResults: "refreshMatches"
    }, function(response){
    });
    
    if (bIsFirstTime) {
        chrome.extension.sendRequest({
            getResults: "initSchedule"
        }, function(response){
        });
    }
    
}

function onChkFollowMyTeamChanged(){
    if ($('#chk_follow_my_team').is(':checked')) 
        localStorage.setObject('follow_my_team', 1);
    else 
        localStorage.setObject('follow_my_team', 0);
    
    chrome.extension.sendRequest({
        getResults: "refreshMatches"
    }, function(response){
    });
    if (bIsFirstTime) {
        chrome.extension.sendRequest({
            getResults: "initSchedule"
        }, function(response){
        });
    }
}

function onChkFollowMyGroupTeamsChanged(){
    if ($('#chk_follow_my_group_teams').is(':checked')) {
        localStorage.setObject('follow_my_group_teams', 1);
    }
    else 
        localStorage.setObject('follow_my_group_teams', 0);
    
    chrome.extension.sendRequest({
        getResults: "refreshMatches"
    }, function(response){
    });
    
    if (bIsFirstTime) {
        chrome.extension.sendRequest({
            getResults: "initSchedule"
        }, function(response){
        });
    }
}

function onChkFollowAllTeamsChanged(){
    if ($('#chk_follow_all_teams').is(':checked')) 
        localStorage.setObject('follow_all_teams', 1);
    else 
        localStorage.setObject('follow_all_teams', 0);
    
    chrome.extension.sendRequest({
        getResults: "refreshMatches"
    }, function(response){
    });
    if (bIsFirstTime) {
        chrome.extension.sendRequest({
            getResults: "initSchedule"
        }, function(response){
        });
    }
}



